#include "SnatchListScene.h"
#include "../../../model/proxy/ChanllengeProxy.h"
#include "../ChallengeItem.h"
#include "../../../net/NetController.h"

SnatchListScene::SnatchListScene(void)
	:mCurTab(TAB_NONE),mScrollView(NULL),mFindBtn(NULL)
{
}

SnatchListScene::~SnatchListScene(void)
{
    CC_SAFE_RELEASE(mFindBtn);
}

bool SnatchListScene::init()
{
	return true;
}

SEL_MenuHandler SnatchListScene::onResolveCCBCCMenuItemSelector(CCObject * pTarget, CCString * pSelectorName )
{
	return NULL;
}

SEL_CCControlHandler SnatchListScene::onResolveCCBCCControlSelector( CCObject * pTarget, CCString * pSelectorName )
{
	CCB_SELECTORRESOLVER_CCCONTROL_GLUE(this, "onRandomTabClick", SnatchListScene::onRandomTabClick);
    CCB_SELECTORRESOLVER_CCCONTROL_GLUE(this, "onEnemyTabClick", SnatchListScene::onEnemyTabClick);
    CCB_SELECTORRESOLVER_CCCONTROL_GLUE(this, "onFindTabClick", SnatchListScene::onFindTabClick);
	return NULL;
}

bool SnatchListScene::onAssignCCBMemberVariable( CCObject * pTarget, CCString * pMemberVariableName, CCNode * pNode )
{
    CCB_MEMBERVARIABLEASSIGNER_GLUE(this, "mFindBtn", CCControlButton *, this->mFindBtn);
	return false;
}

void SnatchListScene::onNodeLoaded( CCNode * pNode, CCNodeLoader * pNodeLoader )
{
	mScrollView = CCScrollView::create(CCSizeMake(320, 270));
	mScrollView->setDirection(CCScrollViewDirectionVertical);
	mScrollView->setPosition(ccp(0, 64));
	addChild(mScrollView);
    mFindBtn->setVisible(false);
}

void SnatchListScene::onEnter()
{
	State::onEnter();
	CCArray* nameList = CCArray::create(ccs(kNCGetChallengeList),ccs(kNCChallengePlayer),ccs(kNCGetChallengeEnemyList),NULL);
	RegisterObservers(this, nameList, callfuncO_selector(SnatchListScene::_onNotification)); 
    changeTab(TAB_RANDOM);
}

void SnatchListScene::onExit()
{
	State::onExit();
}

void SnatchListScene::refresh()
{
	clear();
	const char* ccbi;
	int count = 0;
	if (mCurTab == TAB_RANDOM || mCurTab == TAB_FIND)
		ccbi = "ChallengeItem.ccbi";
	else if (mCurTab == TAB_ENEMY)
		ccbi = "ChallengeEnemyItem.ccbi";
	ChallengeEnemyList& list = ChanllengeProxy::getSingletonPtr()->mChallengeEnemyList;
	ChallengeEnemyList::iterator iter = list.begin();
	while(iter != list.end())
	{
		ChallengeItem* item = (ChallengeItem*)ReadNode(ccbi, this);
		item->setData(*iter);
		item->refresh();
		item->setPosition(ccp(0, 180 - count * 88));
		mScrollView->addChild(item);
		mCurList.push_back(item);
		iter++;
		count++;
	}
    mFindBtn->setVisible(mCurTab == TAB_FIND);
}

void SnatchListScene::_onNotification( CCObject* object )
{
	CCLog("SnatchListScene::_onNotification");
	NotificationObserver* observer = (NotificationObserver*)object;
	std::string name(observer->getName());
	if (name == kNCGetChallengeList || name == kNCGetChallengeEnemyList)
	{
		refresh();
	}
	else if (name == kNCChallengePlayer)
	{
		Framework::getSingletonPtr()->popup("ChallengeResultDialog");
	}
}

void SnatchListScene::onRandomTabClick( CCObject * pSender, CCControlEvent pCCControlEvent )
{
	changeTab(TAB_RANDOM);
}

void SnatchListScene::onEnemyTabClick( CCObject * pSender, CCControlEvent pCCControlEvent )
{
	changeTab(TAB_ENEMY);
}

void SnatchListScene::onFindTabClick( CCObject * pSender, CCControlEvent pCCControlEvent )
{
	clear();
    mFindBtn->setVisible(true);
}

void SnatchListScene::onFindBtnClick( CCObject * pSender, CCControlEvent pCCControlEvent )
{
    std::string name = "";
	getFindName(name);
	if(name == "") return;
	NetController::getSingletonPtr()->findChallengePlayer(name);
}

void SnatchListScene::changeTab( ChallengeTab tab )
{
	if(mCurTab == tab) return;
	mCurTab =  tab;
    if(mCurTab == TAB_RANDOM)
        NetController::getSingletonPtr()->getChallengeList();
    else if(mCurTab == TAB_ENEMY)
        NetController::getSingletonPtr()->getChallengeEnemyList();
}

void SnatchListScene::clear()
{
	for (int i = 0; i < mCurList.size(); i++)
		mCurList[i]->removeFromParentAndCleanup(true);
	mCurList.clear();
}

void SnatchListScene::getFindName( std::string& name )
{
	name = "hello";
}
